package lium.lbs.lbsim.fragments

import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.text.TextUtils
import android.view.View
import com.buz.hjcdriver.bean.GroupModel
import com.chad.library.adapter.base.BaseQuickAdapter
import com.cjt2325.cameralibrary.util.LogUtil
import com.lmlibrary.Constants
import com.lmlibrary.bean.ResponseBean
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.fragment_gropuplist.*
import lium.lbs.lbsim.R
import lium.lbs.lbsim.adapter.GroupListAdapter
import lium.lbs.lbsim.bean.GroupListBean
import lium.lbs.lbsim.jgchat.application.GJApp
import lium.lbs.lbsim.utils.PinnedHeaderItemDecoration
import lium.lbs.lbsim.utils.SpUtils
import lium.lbs.lbsim.utils.callbck.DialogCallback
import java.util.HashMap

class GroupListFragment : BaseFragment(),
    BaseQuickAdapter.OnItemClickListener {
    var list = arrayListOf<GroupModel>()
    var page: Int = 1
    lateinit var listSting: List<String>
    lateinit var adapter: GroupListAdapter
    private val listData: ArrayList<GroupModel> = arrayListOf()
    override fun getLayoutId(): Int {
        return R.layout.fragment_gropuplist
    }

    companion object {
        fun newInstance(): GroupListFragment {
            return GroupListFragment()
        }
    }


    override fun initView() {

        listSting = arrayListOf<String>()
        adapter = GroupListAdapter(listData)
        recyclerView.adapter = adapter
        adapter.disableLoadMoreIfNotFullPage(recyclerView) //关闭BRAVE的自动加载功能 改成 refreshLayout的上拉加载
        adapter.onItemClickListener = this
        val layoutManager = LinearLayoutManager(activity)
        recyclerView.layoutManager = layoutManager
        recyclerView.addItemDecoration(PinnedHeaderItemDecoration())

        refreshLayout.setOnRefreshListener {
            page = 1
            getGroupList();
            refreshLayout.finishRefresh()
            refreshLayout.finishLoadMore()
        }
        refreshLayout.setOnLoadMoreListener {
            refreshLayout.finishRefresh()
            refreshLayout.finishLoadMore()
        }
        getGroupList();
    }

    fun getGroupList() {
        val param = HashMap<String, String>()
        param["memberId"] = SpUtils.getInstance().get(Constants.memberId, "") as String
        param["keywords"] = ""
        param["pageNumber"] = page.toString()
        param["pageSize"] = "15"

        postData(
            "/group/getMyGroupList",
            param,
            object : DialogCallback<ResponseBean<GroupListBean>>(baseActivity) {
                override fun onSuccess(response: Response<ResponseBean<GroupListBean>>) {
                    setDataGroupList(response.body().data)
                }
            })
    }


    fun setDataGroupList(groupListBean: GroupListBean) {
        list.clear()
        if (groupListBean.myCreatelist === null && groupListBean.myJoinList == null) {
            if (page > 1) {
                page--;
            }
        } else {
            if (groupListBean.myCreatelist != null) {
                if (groupListBean.myCreatelist.size > 0) {
                    list.add(GroupModel("", listSting, "我创建的群", "", "", GroupModel.ITEM_TYPE_SETION))
                }
                for (i in 0..groupListBean.myCreatelist.size - 1) {
                    list.add(
                        GroupModel(
                            groupListBean.myCreatelist.get(i).name,
                            groupListBean.myCreatelist.get(i).headImg,
                            groupListBean.myCreatelist.get(i).name,
                            groupListBean.myCreatelist.get(i).id,
                            groupListBean.myCreatelist.get(i).gid,
                            GroupModel.ITEM_TYPE_DATA
                        )
                    )
                }

            }
            if (groupListBean.myJoinList != null) {
                if (groupListBean.myJoinList.size > 0) {
                    list.add(GroupModel("", this!!.listSting, "全部", "", "", GroupModel.ITEM_TYPE_SETION))

                    for (i in 0..groupListBean.myJoinList.size - 1) {
                        list.add(
                            GroupModel(
                                groupListBean.myJoinList.get(i).name,
                                groupListBean.myJoinList.get(i).headImg,
                                groupListBean.myJoinList.get(i).name,
                                groupListBean.myJoinList.get(i).id,
                                groupListBean.myJoinList.get(i).gid,
                                GroupModel.ITEM_TYPE_DATA
                            )
                        )
                    }

                }
            }
        }

        if (page == 1) {
            adapter.data.clear()
        }
        adapter.data.addAll(list)
        adapter.notifyDataSetChanged()
    }

    override fun onClick(v: View?) {
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {


        if (!TextUtils.isEmpty(list.get(position).id)) {
            val intent = Intent()
            intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
            //设置跳转标志
            intent.putExtra("fromGroup", false)
            intent.putExtra(GJApp.MEMBERS_COUNT, 0 + 1)
            intent.putExtra(GJApp.GROUP_ID, (list.get(position).gid).toLong())
            intent.putExtra(GJApp.MY_GROUP_ID, list.get(position).id)
            intent.putExtra(GJApp.CONV_TITLE, list.get(position).displayName)
            intent.setClass(activity, lium.lbs.lbsim.jgchat.activity.ChatActivity::class.java)
            startActivityForResult(intent, 100)
        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        LogUtil.e("onActivityResult--resultCode" + data + "requestCode" + requestCode)
        if (requestCode == 100 && resultCode == 200) {
        }
        if (requestCode == 100 && resultCode == 203) {
            LogUtil.e("onActivityResult--" + 203 + "body")
            getGroupList()
        }
    }

}